package lab3;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * Mex4 - basic threading in GUIs - still working on it
 * @author Anthony Varghese
 */

/**
 * Mex4Window - window (JFrame) class for threading
 */
class Mex4Window extends JFrame {
	/**
	 * Data members
	 * serialVersionUID - declaring and assigning a value prevents warning messages.
	 * origTitle - title for the window
	 * jContentPane
	 * jPanel
	 * jButton
	 */
	private static final long serialVersionUID = 1L;
	private String origTitle = new String("Mex4 window");
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JButton jButton = null;
	private int panelwidth = 300;
	private int panelheight = 180;

	/**
	 * Data members describing the bouncing ball
	 */
	private final int OVAL_WIDTH = 10;
	private final int OVAL_HEIGHT = 10;
	private PongBall pong;

	/**
	 * Default constructor
	 */
	public Mex4Window() {
		super();
		initialize();
	}

	/**
	 * initialize - Initialize the MexWindow object
	 * 
	 * @return void
	 */
	private void initialize() {
		/**
		 * Initialize the jButton
		 */
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Start");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (g2d == null) return;
					/*
					 * Fire up a PongBall and move it.
					 */
					pong = new PongBall( 1 );
					java.awt.Point old = new java.awt.Point( );
					java.awt.Point pos = pong.getLocation();
					for (int i=0; i<1000; i++){
						System.out.println(pong);
						/*
						 * Find the relative location in the panel
						 * of the old position and erase the pong ball
						 */
						old.x = pos.x; old.y = pos.y;
						int scaled_x = old.x * panelwidth / 100;
						int scaled_y = old.y * panelheight / 100;
						g2d.setColor( g2d.getBackground() ); // painting using background == erase
						g2d.fillOval(scaled_x, scaled_y, OVAL_WIDTH, OVAL_HEIGHT);
						
						
						pong.move();
						pos = pong.getLocation();

						/*
						 * Find the relative location in the panel
						 * of the new position and draw the pong ball
						 */
						scaled_x = pos.x * panelwidth / 100;
						scaled_y = pos.y * panelheight / 100;
						g2d.setColor( pong.getColor() );
						g2d.fillOval(scaled_x, scaled_y, OVAL_WIDTH, OVAL_HEIGHT);
					}
				}
			});
		}

		/**
		 * Initialize the jPanel
		 */
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setMinimumSize(new Dimension(panelwidth, panelheight));
			jPanel.setPreferredSize(new Dimension(panelwidth, panelheight));
			jPanel.setBackground(Color.black);
		}
		/**
		 * Initialize the jContentPane
		 */
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new FlowLayout());
			jContentPane.add(jButton, null);
			jContentPane.add(jPanel, null);
		}
		setContentPane( jContentPane );
		setTitle(origTitle);
		setPreferredSize(new Dimension(400, 300));
		setBounds(new Rectangle(200, 100, 400, 300));
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}

	/*
	 * Graphics surface for drawing
	 */
	private java.awt.Graphics2D g2d;

	/*
	 * Paint method for the window
	 * 
	 * @see java.awt.Container#paint(java.awt.Graphics)
	 */
	public void paint(java.awt.Graphics g) {
		super.paint(g);

		/*
		 * Get the window size and change the size of the panel if the user has
		 * resized the window.
		 */
		System.out.println("paint called. Frame width = " + getWidth() + " and height = " + getHeight() );
		jPanel.setSize((int)(0.8*getWidth()), (int)(0.8*getHeight()) );
		panelwidth = jPanel.getWidth();
		panelheight = jPanel.getHeight();
		System.out.println("              Panel width = "  + panelwidth + " and height = " + panelheight );

		/*
		 * g2d is set here and used to draw the pong balls when the user clicks
		 * the Start button.
		 */
		g2d = (java.awt.Graphics2D) jPanel.getGraphics();

	}
}

/*
 * Class for the ball that should bounce around
 */
class PongBall{
	private int id;

	private java.awt.Point current_position = new java.awt.Point(50, 50);
	private java.awt.Point previous_position = new java.awt.Point(-10, -10);
	private java.awt.Color color = new java.awt.Color(
			127 + (int) (Math.random() * 128),
			127 + (int) (Math.random() * 128),
			127 + (int) (Math.random() * 128));
	private final long sleepInterval = 500; // milliseconds
	private int x_incr;
	private int y_incr;

	/**
	 * Constructor
	 */
	public PongBall(int i){
		id = i;
		/*
		 * Set the initial x- and y-speeds to something that is not 0
		 */
		do	x_incr = (int) ((Math.random() - 0.5) * 10); while (x_incr == 0);
		do 	y_incr = (int) ((Math.random() - 0.5) * 10); while (y_incr == 0);
	}
	
	public java.awt.Color getColor() {
		return color;
	}

	public java.awt.Point getPreviousLocation() {
		return previous_position;
	}

	public java.awt.Point getLocation() {
		return current_position;
	}

	public void move() {
		// remember the old position
		previous_position.x = current_position.x;
		previous_position.y = current_position.y;
		
		// update new position
		current_position.x = current_position.x + x_incr;
		current_position.y = current_position.y + y_incr;

		// wait a bit
		try {
			Thread.sleep(sleepInterval);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
	}
	
	public String toString(){
		return " Pong ball # " + id + " color: " + color + " at " + current_position;
	}
}


/*
 * Main class for this program
 */
public class Mex4{
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		(new Mex4Window()).setVisible( true );
	}
}
